package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.BatchDto;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.ManageRule;
import com.jyy.prefabricated.service.ManageRuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 管理制度
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class ManageRuleController {

    @Autowired
    private ManageRuleService manageRuleService;

    /**
     * 新增制度
     * @param manageRule
     * @return
     */
    @PostMapping("/manage-rule")
    public AjaxResult insert(@Valid @RequestBody ManageRule manageRule){
        log.info("Rest request to insert manageRule:{}",manageRule);
        int id = manageRuleService.insert(manageRule);
        ManageRule result = manageRuleService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 更新制度
     * @param manageRule
     * @return
     */
    @PutMapping("/manage-rule")
    public AjaxResult update(@Valid @RequestBody ManageRule manageRule){
        log.info("Rest request to update manageRule:{}",manageRule);
        manageRuleService.update(manageRule);
        ManageRule result = manageRuleService.selectById(manageRule.getId());
        return AjaxResult.success(result);
    }

    /**
     * 批量修改状态
     * @param dto
     * @return
     */
    @PutMapping("/manage-rule-batch")
    public AjaxResult update(@Valid @RequestBody BatchDto dto){
        log.info("Rest request to batch-update manageRule:{}",dto);
        manageRuleService.batchUpdateStatus(dto);
        return AjaxResult.success();
    }

    /**
     * 批量删除
     * @param dto
     * @return
     */
    @DeleteMapping("/manage-rule-batch")
    public AjaxResult deleteById(@RequestBody BatchDto dto){
        log.info("Rest request to batch-delete manageRule by id:{}",dto);
        int row = manageRuleService.batchDelete(dto);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 查询上一条制度
     * @param id
     * @return
     */
    @GetMapping("/manage-rule-prev/{id}")
    public AjaxResult selectByPrev(@PathVariable Integer id){
        log.info("Rest request to find manageRule by prev id:{}",id);
        List<ManageRule> result = manageRuleService.selectByPage(id,"prev");
        return AjaxResult.success(result);
    }

    /**
     * 查询下一条制度
     * @param id
     * @return
     */
    @GetMapping("/manage-rule-next/{id}")
    public AjaxResult selectByNext(@PathVariable Integer id){
        log.info("Rest request to find manageRule by next id:{}",id);
        List<ManageRule> result = manageRuleService.selectByPage(id,"next");
        return AjaxResult.success(result);
    }

    /**
     * 查询制度详情
     * @param id
     * @return
     */
    @GetMapping("/manage-rule/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to find manageRule by id:{}",id);
        ManageRule result = manageRuleService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除制度
     * @param id
     * @return
     */
    @DeleteMapping("/manage-rule/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        log.info("Rest request to delete manageRule by id:{}",id);
        int row = manageRuleService.delete(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 分页查询制度
     * @param categoryId 分类id
     * @param title 标题
     * @param status 状态
     * @param pageable 分页信息
     * @return
     */
    @GetMapping("/manage-rule")
    public AjaxResult selectByParams(@RequestParam(required = false) Integer categoryId, @RequestParam(required = false, defaultValue = "") String title, @RequestParam(required = false) Integer status, Pageable pageable){
        log.info("Rest request to find manageRule by params categoryId:{}, title:{}, status:{}",categoryId, title, status);
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<ManageRule> manageRule = manageRuleService.selectByParams(categoryId, title, status);
        PageInfo<ManageRule> result = new PageInfo<>(manageRule);
        return AjaxResult.success(result);
    }

    /**
     * 查询制度
     * @param categoryId 分类id
     * @param title 标题
     * @param status 状态
     * @return
     */
    @GetMapping("/manage-rule-list")
    public AjaxResult selectByParams(@RequestParam(required = false) Integer categoryId, @RequestParam(required = false, defaultValue = "") String title, @RequestParam(required = false) Integer status){
        log.info("Rest request to find manageRule by params categoryId:{}, title:{}, status:{}",categoryId, title, status);
        List<ManageRule> result = manageRuleService.selectByParams(categoryId, title, status);
        return AjaxResult.success(result);
    }
}
